
// Copyright (C) 2007 Richard Uhler
// This file is part of Esiply.
//
// Esiply is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, Version 2,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <iostream>

#include "Completion.h"
#include "Statement.h"
#include "String.h"

Statement::Statement()
    : m_line_number(-1)
{}

Value* Statement::execute(Scope* scope) const
{
    // Ecma 12.12
    // Execute the statement. If it returns (break, v, l) where we have
    // l as a current lable, return (normal, v, empty)
    Value* res = execute_internal(scope);

    // Check for an exception while we're at it
    // TODO: this should be more formal.
    if (res->isException())
    {
        std::cerr << "Exception at line " << m_line_number << std::endl;
    }
        
    if (res->getType() == VT_COMPLETION
            && res->completion()->type() == CT_BREAK
            && this->hasLabel(res->completion()->target()))
    {
        return new Completion(CT_NORMAL, res->completion()->value(), Completion::empty);
    }
    return res;
}

void Statement::getDeclaredVariables(std::vector<String*>& ids) const
{}

bool Statement::hasLabel(String* target) const
{
    return m_labels.find(target) != m_labels.end();
}

void Statement::addLabel(String* id)
{
    m_labels.insert(id);
}

void Statement::setLineNumber(int line)
{
    m_line_number = line;
}

int Statement::getLineNumber() const
{
    return m_line_number;
}

